package org.bigjava.controller;

import org.bigjava.bean.User;
import org.bigjava.bean.Xiaoqu;
import org.bigjava.biz.UserService;
import org.bigjava.biz.XiaoquService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    HttpSession session;

    @Autowired
    UserService userService;
    @Autowired
    XiaoquService xiaoquService;

    //业主账号校验
    @RequestMapping("user/jiaoyan")
    @ResponseBody
    public User userJiaoYan(String username){
        User user = userService.userJiaoYan(username);
        User usera = (User) session.getAttribute("user");
        if(user!=null){
            if(user.getId()==usera.getId()){
                return null;
            }
        }
        return user;
    }

    //搜索业主
    @RequestMapping("user/sousuo")
    public String userSouSuo(String dyg,String deg){
        List<User> lists = userService.userMoHu(dyg,deg);
        session.setAttribute("users",lists);
        return "yzgl";
    }

    //修改业主
    @RequestMapping("user/xiugai")
    @ResponseBody
    public String userXiuGai(User user){
        User usera = (User) session.getAttribute("user");
        user.setId(usera.getId());
        int i = userService.userXiuGai(user);
        if(i>0){
            return "OK";
        }else{
            return "修改失败";
        }
    }

    //增加业主
    @RequestMapping("user/tianjia")
    @ResponseBody
    public String userTianJia(User user){
        int i = userService.userAdd(user);
        if(i>0){
            return "OK";
        }else{
            return "添加失败";
        }
    }
    //跳转到业主修改界面(拿到小区列表和业主信息)
    @RequestMapping("user/xg")
    public String yzxg(int id){
        User user = userService.userId(id);
        session.setAttribute("user",user);
        List<Xiaoqu> lists = xiaoquService.xiaoQuQuan();
        session.setAttribute("xiaoqus",lists);
        return "yzxg";
    }
    //跳转到业主添加界面(拿到小区列表)
    @RequestMapping("user/yztj")
    public String yztj(){
        List<Xiaoqu> lists = xiaoquService.xiaoQuQuan();
        session.setAttribute("xiaoqus",lists);
        return "yztj";
    }
    //业主删除
    @RequestMapping("user/shanchu")
    public String userShanChu(int id){
        userService.userShanChu(id);
        //拿到当前页数
        Object a = session.getAttribute("yeshu");
        return "redirect:/user/fenye?a="+a;
    }
    //业主分页查询
    @RequestMapping("/user/fenye")
    public String userFenYe(String a){
        //业主总人数
        int zong = userService.userZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a,Constant.fenye);
        //分页查询出的业主
        List<User> lists = userService.userFenYe(ks,Constant.fenye);
        session.setAttribute("users",lists);
        return "yzgl";
    }
    //业主登录
    @RequestMapping("user/denglu")
    @ResponseBody
    public String userDengLu(String username, String password){
        User user = userService.userDengLu(username,password);
        if(user == null){
            return "登录失败";
        }else{
            session.setAttribute("user",user);
            return "yezhu.html";
        }
    }
}